﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.RegInfo.Models;


namespace BusinessLayer.RegInfo
{
    public class AdminActionBL
    {
        private readonly DbHelper _db;

        public AdminActionBL()
        {
            _db = new DbHelper(new DbConfig().DbRegInfoConnectionString);
        }

        public List<AdminActionExtend> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllAdminActions) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<AdminActionExtend>(oCommand);
            return list;
        }
        public int CreateAdminAction(AdminAction action)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminAction) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@Code", action.Code));
                oCommand.Parameters.Add(new SqlParameter("@Name", action.Name));
                oCommand.Parameters.Add(new SqlParameter("@ActionGroupID", action.ActionGroupID));
               

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminAction(AdminAction action)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateAdminAction) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@ActionID", action.ActionID));
                oCommand.Parameters.Add(new SqlParameter("@Code", action.Code));
                oCommand.Parameters.Add(new SqlParameter("@Name", action.Name));
                oCommand.Parameters.Add(new SqlParameter("@ActionGroupID", action.ActionGroupID));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminActionExtend GetByActionCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminActionByCode) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Code", code));
            var list = _db.GetList<AdminActionExtend>(oCommand);
            return list.FirstOrDefault();
        }
        public AdminActionExtend GetByActionID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminActionByID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@ActionID", id));
            var list = _db.GetList<AdminActionExtend>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteAdminActionByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM AdminActions WHERE ActionID IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        //GetAdminActionByActionGroupID
        public List<AdminAction> GetAdminActionsByUserGroupID(int usergroupid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminActionByUserGroupID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));
            var list = _db.GetList<AdminAction>(oCommand);
            return list;
        }
        //sp_AdminActions_Select_By_UserID
        public List<AdminAction> GetAdminActionsByUserID(int userid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminActionByUserID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserID", userid));
            var list = _db.GetList<AdminAction>(oCommand);
            return list;
        }
        //  sp_AdminActions_Select_By_UserName
        public List<AdminAction> GetAdminActionsByUserName(string username)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminActionByUserName) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserName", username));
            var list = _db.GetList<AdminAction>(oCommand);
            return list;
        }
    }
}
